package com.cnfangmao.masterdata.controller;


import java.util.List;











import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.dto.PermissionDto;
import com.cnfangmao.masterdata.dto.RoleDto;
import com.cnfangmao.masterdata.dto.RolePermissionDto;
import com.cnfangmao.masterdata.model.Permission;
import com.cnfangmao.masterdata.model.Role;
import com.cnfangmao.masterdata.model.RolePermission;
import com.cnfangmao.masterdata.service.RoleService;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-17
 */
@RequestMapping("/role")
@RestController()
@Api(description = "RoleController->角色权限", tags = "RolePermission")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	 /**
     * 获取角色列表
     */
    @RequestMapping(value="/list.do", method = RequestMethod.GET)
    @ApiOperation(value="角色列表", notes = "")
    @ApiImplicitParams({
      	 @ApiImplicitParam(name = "rolename", value = "角色名称", paramType = "query", dataType = "string"),
      	 @ApiImplicitParam(name = "pageNo", value = "当前页", paramType = "query", dataType = "int"),
      	 @ApiImplicitParam(name = "pageSize", value = "显示条数", paramType = "query", dataType = "int")
      })
    @RequiresPermissions("role:list")
    public ResBody listRole( String rolename,
    		@RequestParam(defaultValue="1",required=false) int pageNo,
    		@RequestParam(defaultValue="10",required=false) int pageSize)
    {
    	ResBody resBody = new ResBody();
    	IPage<Role> data =roleService.queryListRole(rolename, pageNo, pageSize);
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(data);
        resBody.message("查询成功");
    	return resBody;
    }
    
    /**
     * 创建角色
     */
    @RequestMapping(value="/create.do", method = RequestMethod.POST)
    @ApiOperation(value="创建角色", notes = "")
    @ApiImplicitParam(name = "input", value = "创建角色",required = true, paramType = "body", dataType = "RoleDto")
    @RequiresPermissions("role:add")
    public ResBody createRole(@Validated @RequestBody  @NotNull RoleDto input){
    	ResBody resBody = new ResBody();
    	if(input.getRoleName().isEmpty())
    	{
    		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
        	resBody.message("角色名为空");
            return resBody;
    	}
    	if(roleService.findByRoleName(input.getRoleName())!=null)
    	{
    		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
        	resBody.message("角色名已经存在");
            return resBody;
    	}
    	roleService.save(input, "");
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(true);
        resBody.message("创建成功");
    	return resBody;
    }
    
    /**
     * 更新角色
     */
    @RequestMapping(value="/update.do", method = RequestMethod.POST)
    @ApiOperation(value="更新角色", notes = "")
    @ApiImplicitParam(name = "input", value = "更新角色信息",required = true, paramType = "body", dataType = "RoleDto")
    @RequiresPermissions("role:update")
    public ResBody updateRole(@Validated @RequestBody  @NotNull RoleDto input){
    	ResBody resBody = new ResBody();
    	if(input.getRoleName().isEmpty())
    	{
    		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
        	resBody.message("角色名为空");
            return resBody;
    	}
    	Role role=roleService.findByRoleName(input.getRoleName());
    	if(role!=null&&!role.getRoleId().equals(input.getRoleId()))
		{
    		resBody.code(ResCodeEnum.PARAM_NULL.getCode());
        	resBody.message("角色名已经存在");
            return resBody;
		}
    	roleService.update(input, "");
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(true);
        resBody.message("修改成功");
    	return resBody;
    }

    /**
     * 删除角色
     */
    @RequestMapping(value="/delete.do", method = RequestMethod.POST)
    @ApiOperation(value="删除角色", notes = "")
    @ApiImplicitParam(name = "roleId", value = "角色roleId", required = true, dataType = "String")
    @RequiresPermissions("role:delete")
    public ResBody deleteRole(@Validated @RequestParam @NotNull String roleId){
    	ResBody resBody = new ResBody();
    	roleService.delete(roleId);
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(true);
        resBody.message("删除成功");
    	return resBody;
    }
    
    /**
     * 获取角色详情
     */
    @RequestMapping(value="/getRole.do", method = RequestMethod.GET)
    @ApiOperation(value="获取角色详情", notes = "")
    @ApiImplicitParam(name = "roleId", value = "角色roleId", required = true, dataType = "String")
    @RequiresPermissions("role:getRole")
    public ResBody getRole(@Validated @RequestParam @NotNull String roleId){
    	ResBody resBody = new ResBody();
    	Role role=roleService.getById(roleId);
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(role);
        resBody.message("获取角色成功");
    	return resBody;
    }
    
    /**
     * 获取所有权限数据
     */
    @RequestMapping(value="/getPermission.do", method = RequestMethod.GET)
    @ApiOperation(value="获取所有权限数据", notes = "")
    @RequiresPermissions("role:getAllPermission")
    public ResBody getAllPermission(){
    	ResBody resBody = new ResBody();
    	List<PermissionDto> permission =roleService.getAllPermissions();
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(permission);
        resBody.message("获取所有权限成功");
    	return resBody;
    }
    
    /**
     * 获取所有角色权限对应数据
     */
    @RequestMapping(value="/getRolePermission.do", method = RequestMethod.GET)
    @ApiOperation(value="获取所有角色权限对应数据", notes = "")
    @ApiImplicitParam(name = "roleId", value = "角色roleId", required = true, dataType = "String")
    @RequiresPermissions("role:getRolePermission")
    public ResBody getRolePermission(@Validated @RequestParam @NotNull String roleId){
    	ResBody resBody = new ResBody();
    	List<RolePermissionDto> rolePermission=roleService.getRolePermission(roleId);
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(rolePermission);
        resBody.message("获取所有角色权限对应数据成功");
    	return resBody;
    }

    /**
     * 获取所有角色
     */
    @RequestMapping(value="/getAllRoles.do", method = RequestMethod.GET)
    @ApiOperation(value="获取所有角色", notes = "")
    @RequiresPermissions("role:getAllRoles")
    public ResBody getAllRoles(){
    	ResBody resBody = new ResBody();
    	List<Role> roles =roleService.getAllRoles();
    	resBody.code(ResCodeEnum.SUCCESS.getCode());
        resBody.data(roles);
        resBody.message("获取所有角色成功");
    	return resBody;
    }

}

